package com.bolt.core.view.components;


import com.bolt.common.utils.StrUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *  * 用于jquery ValidatorRule
 *  @author  mi
 */
public class ValidatorRule {
	private  Form parent;
	private  String id;
    private  String value;
	private  Map<String,String> validatorValue;
	private  List<String> message;
	
	public ValidatorRule(){
		validatorValue = new LinkedHashMap<String,String>();
		message = new ArrayList<String>();
	}
	
	public ValidatorRule(Form parent){
		this.parent = parent;
	}
	
	public Form getParent() {
		return parent;
	}

	public void setParent(Form parent) {
		this.parent = parent;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Map<String,String>  getValidatorValue() {
		return validatorValue;
	}

	public void andValidatorValue(String validatorRole,String validatorValue) {
		this.validatorValue.put(validatorRole, validatorValue);
	}

	public List<String> getMessage() {
		return message;
	}

	public void setMessage(List<String> message) {
		this.message = message;
	}
	public void addMessage(String message){
		this.message.add(message);
	}
	public void handleValidatorValue(String value){
		if(StrUtil.equalsIgnoreCase(value, "*")){//不可为空
			andValidatorValue("required","true"); 
		}else if(StrUtil.equalsIgnoreCase(value, "N")){//数值
			andValidatorValue("number","true");
		}else if(StrUtil.equalsIgnoreCase(value, "E")){//邮件
			andValidatorValue("email","true");
		}else if(StrUtil.equalsIgnoreCase(value, "D")){//日期
			andValidatorValue("date","true");
		}else if(StrUtil.equalsIgnoreCase(value, "Z")){//必须输入整数
			andValidatorValue("digits","true");
		}else{
			String[]temp = StrUtil.split(value, ":");
			if(temp.length>1){
				andValidatorValue(temp[0],temp[1]);
			}else{
				andValidatorValue(temp[0],"true");
			}

		}
	}
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

	/*
	public String toString(){
		StringBuilder sb = new StringBuilder();
		Set<String> validatorRoles = validatorValue.keySet();
		int i=0;
		for(String validatorRole : validatorRoles){
			sb.append(validatorRole +" = \"" + validatorValue.get(validatorRole)+"\" ");
			if(message.size() > i){
				sb.append("title= \"" + message.get(i)+"\" ");
			}
			i++;
		}
		return sb.toString();
		
	}
	*/

}
